#include "ResourceManager.h"
#include "DxUtils.h"
#include "ImageLoader.h"

using namespace PracEng;
using namespace std;

static hash<wstring> GetHash;

D3DTexResource::~D3DTexResource()
{
	SAFE_RELEASE(pSRV);
}

ResourceManager::~ResourceManager()
{
	for (auto it : m_mapShaderResource)
	{
		delete it.second;
	}
}

void ResourceManager::GetTexture(const wchar_t* Src, DX11Renderer* Dx11Render, ID3D11ShaderResourceView** textureView, UINT forceFmt)
{
	size_t hashCode = GetHash(Src);

	auto it = m_mapShaderResource.find(hashCode);
	if (it != m_mapShaderResource.end())
	{
		*textureView = it->second->pSRV;
	}
	else
	{
		D3DTexResource* Res = new D3DTexResource();
		ImageLoader::CreateWICTextureFromFile(Dx11Render->GetDevice(), Dx11Render->GetDeviceContext(), Src, NULL, &(Res->pSRV), 4096, forceFmt);
		*textureView = Res->pSRV;
		m_mapShaderResource.insert(pair<size_t, D3DTexResource*>(hashCode, Res));
	}
}